/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boxin.ims.modules.wechat.dao.TenpayDao;
import com.boxin.ims.modules.wechat.entity.Tenpay;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;

/**
 * 财付通支付Service
 * @author Jakemanse
 * @version 2013-09-21
 */
@Component
@Transactional(readOnly = true)
public class TenpayService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(TenpayService.class);
	
	@Autowired
	private TenpayDao tenpayDao;
	
	public Tenpay get(Long id) {
		return tenpayDao.findOne(id);
	}
	
	
	public Tenpay getByUserId(Long userId){
		DetachedCriteria dc = tenpayDao.createDetachedCriteria();
		dc.add(Restrictions.eq("user.id", userId));
		List<Tenpay> result = tenpayDao.find(dc);
		
		return (result == null || result.isEmpty()) ? null : result.get(0);
		
	}
	
	public Page<Tenpay> find(Page<Tenpay> page, Tenpay tenpay) {
		DetachedCriteria dc = tenpayDao.createDetachedCriteria();
		if (tenpay.getUser()!=null && tenpay.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", tenpay.getUser().getId()));
		}
		
		if (StringUtils.isNotEmpty(tenpay.getQq())){
			dc.add(Restrictions.like("qq", "%"+tenpay.getQq()+"%"));
		}
		
		if (StringUtils.isNotEmpty(tenpay.getAppid())){
			dc.add(Restrictions.eq("appid",  tenpay.getAppid() ));
		}
		
		if (StringUtils.isNotEmpty(tenpay.getRemarks())){
			dc.add(Restrictions.like("remarks", "%"+tenpay.getRemarks()+"%"));
		}
		dc.add(Restrictions.eq("delFlag", Tenpay.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return tenpayDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(Tenpay tenpay) {
		tenpayDao.save(tenpay);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		tenpayDao.deleteById(id);
	}
	
}
